/*	kd:
	
	Using the shop code, you can code a quick select like this, too.
	
	*/

class Gcq_QuickSelect play {
	
	// Base data stuff
	protected Gcq_SelectableItems	items;
	protected GcSpellEquip			player_spells;
	protected int					items_index;
	
	// kd: Layout and drawing
	protected Gc_Shop_InvLayout		ring_layout;
	protected Gc_Shop_DrawCache		cache;
	protected Gc_Shop_UiHandling	ui_handling;
	
	// Player
	protected Actor					user;
	protected PlayerInfo			player_info;
	protected vector2				locked_ang;

	// Ring stuff
	enum animation_state {
		state_idle = 0
	,	state_in
	,	state_out
	}
	
	protected bool					is_active;
	protected animation_state		anim_state;
	protected int					size;
	
	// Drawing stuff
	const	name_color_in			= Font.cr_white;
	const	name_color_final		= Font.cr_sapphire;
	
	const	anim_tics				= 7;
	const	anim_rate				= 1.0 / anim_Tics;
	
	protected Gcq_AmountText		amount_text;
	protected Gc_Shop_DrawCache		draw_cache;
	protected Gcs_ButtonSprites		button_sprites;
	protected Gc_NormalFades		ring_offsets;
	
	protected int					cur_anim_tic;
	
	protected int					fader_anim;
	protected int					fader_alpha;
	
	protected TextureId				cursor_sprite;
	
	bool IsOpen () const {
		return is_active;
	}

	protected vector2					window_resolution;
	protected vector2					sprite_resolution;
	protected vector2					button_resolution;
	protected vector2					text_resolution;
	protected vector2					window_to_sprite;
	protected vector2					button_to_text;
	
	// kd: Centre message:
	protected Gc_RealCounter			name_counter;
	protected String					name_str;
	protected vector2					name_pos;
	protected FadeTick					name_t;
	protected FadeTick					name_fade_t;
	
	// kd: This time I'm not splitting the code up like in the shop.
	protected FadeTick					layout_t;
	
	const anim_duration_tics	= 7;
	const name_fade_tics		= 5;
	
	double LayoutF (double t = 1) const {
		return 0.5 + 0.5 * layout_t.RenderStandardDelta(anim_duration_tics, t);
	}
	
	bool OnBuild () {
		if(!ui_handling.Build()) {
			return false;
		}
		
		button_sprites.Cache();
		ring_layout.Reposition(
			normal_origin:			(0.5, 0.5),
			inner_rad:				0.21,
			outer_rad:				91337.69420,
			draw_rad:				0.35337,
			draw_offset:			0.04);
		return true;
	}
	
	const max_scale_count       = 6;
	const min_scale_count       = 20;
	const max_button_scale      = 1.1;
	const min_button_scale      = 1.0 / 0.777;
	
	protected TextureId				sprite_cursor;
	
	bool IsActive () const {
		return is_active;
	}
	
	// This happens whenever we open the selection.
	bool OnOpen (
	PlayerInfo			user_player,
	Gcq_SelectableItems	item_list,
	int					index) {
		
		// kd: If there are no items matching in your inventory, this will
		// notify the caller it's not opening.
		item_list.FilterByInventory(user_player.mo);
		
		if(item_list.InventorySize() < 1) {
			return false;
		}
		
		items_index = index;
		
		// kd: Don't display any name.
		name_t.Force(0);
		name_fade_t.Force(0);
		
		// kd: Some resolution shenanigans, same as the shop.
		let window_aspect	= 1.0 * Screen.GetWidth() / Screen.GetHeight();
		window_resolution	= 720 * (window_aspect, 1);
		sprite_resolution	= 480 * (window_aspect, 1);
		text_resolution		= 480 * (window_aspect, 1);
		
		window_to_sprite	= (
			sprite_resolution.x / window_resolution.x,
			sprite_resolution.y / window_resolution.y);
		
		player_info			= user_player;
		user				= player_info.mo;
		
		ui_handling.InitLayout(
			// user_player,
			window_resolution,
			sprite_resolution);
		
		ring_layout.InitForCursor(window_resolution);
		
		// kd: Buttons of the player's INTERESTING inventory state.
		items				= item_list;
		items.FilterByInventory(user);
		size				= items.InventorySize();
		
		ring_layout.ClearAndResize(size);
		ring_layout.ClearPositions();
		ring_layout.Rescale(clamp(
			Gc_Interpol.Line(
				size,
				max_scale_count,
				max_button_scale,
				min_scale_count,
				min_button_scale),
			max_button_scale,
			min_button_scale) );
		ring_layout.NextFrameNoRot(LayoutF());
		
		button_resolution	= ring_layout.Resolution();
		button_to_text		= (
			text_resolution.x / button_resolution.x,
			text_resolution.y / button_resolution.y);
		
		ring_offsets.ClearAndResize(size);
		ring_offsets.Reset();
		
		cache.ClearAndResize(size);
		cache.FillFromQuickSelect(player_info, item_list, button_sprites);
		
		// amount_text.ClearAllReserve(size);
		amount_text.MakeForInventory(user, item_list);
		amount_text.SetOffsets(smallfont, 0.5);
		
		// kd: Ring goes in.
		layout_t.To(anim_duration_tics);
		
		sprite_cursor = Gc_MenuPreference.Cursor(player_info);
		
		is_active	= true;
		wants_quit	= false;
		Gc_Shop_ToWorld.SendNetworkEvent("NoTurning");
		Gcs_ShopSounds.QuickStart();
		return true;
	}
	
	protected void LoadPreferences () {
		cursor_sprite = Gc_MenuPreference.Cursor(player_info);
	}

	// Called from outside when the user made a choice (by letting go off the
	// corresponding button).
	void Close () {
		wants_quit = true;
		
		if(ui_handling.IsInsideRing()) {
			Gc_Shop_ToWorld.SendNetworkEvent(
				"letemhaveitmac",
				items_index,
				items.Found(ui_handling.CurrentItem()) );
			// items.Use(user, ui_handling.CurrentItem());
		}
		
		name_fade_t.ContinueTo(0);
		name_t.ContinueTo(0);
		Gc_Shop_ToWorld.SendNetworkEvent("ResetTurning");
	}

	void Tick () {
		if(!is_active) {
			return;
		}
		
		ui_handling.SetCanSelectItems(true);
		ui_handling.NextState(player_info, ring_layout);
		
		if(ui_handling.HasHoveredItem()) {
			let item = ui_handling.CurrentItem();
			
			ring_offsets.ToEnd(item, tics: 6);
			
			// kd: Cache name and all.
			name_str	= items.RealName(item);
			name_pos	= 0.5 * text_resolution - (0.5 * bigfont.StringWidth(name_str), 0);
			name_counter.To(1337, step_: 1.777);
			name_t.Force(name_fade_tics);
			name_fade_t.To(0, from: name_fade_tics);
		}
		
		if(ui_handling.HasLeftRing()) {
			ring_offsets.ToStart(ui_handling.PreviousItem(), tics: 13);
			name_t.ContinueTo(0);
		}
		
		if(ui_handling.HasDehoveredItem()) {
			ring_offsets.ToStart(ui_handling.PreviousItem(), tics: 13);
		}
		
		if(wants_quit) {
			layout_t.ContinueTo(0);
		}
		
		name_t.Next();
		name_fade_t.Next();
		
		ring_offsets.NextState();
		
		layout_t.Next();
		name_counter.Next();
		
		ring_layout.CacheOldPositions();
		ring_layout.NextFrameNoRot(LayoutF());
		
		if(layout_t.Now() < 1) {
			is_active = false;
		}
	}

	// Draw the entire layout, but only if active
	ui void Draw (RenderEvent event) const {
		if(!is_active) {
			return;
		}
		
		let t		= event.fractic;
		let alpha	= layout_t.RenderStandardDelta(anim_duration_tics, t);
		
		ring_layout.BeginDraw(ring_offsets, t);
		ring_layout.DrawButtons(
			cache,
			button_sprites,
			alpha);
		ring_layout.DrawHighlight(
			cache,
			button_sprites,
			alpha);
		ring_layout.DrawIcons(
			cache,
			alpha);
		ring_layout.DrawOverlays(
			cache,
			button_sprites,
			alpha);
		
		let cursor_pos = ui_handling.mouse_input.DrawPos(t);
		
		cursor_pos = (
			cursor_pos.x * window_to_sprite.x,
			cursor_pos.y * window_to_sprite.y);
		
		Screen.DrawTexture(
			sprite_cursor,
			true,
			cursor_pos.x,
			cursor_pos.y,
			DTA_KEEPRATIO,			true,
			DTA_VIRTUALWIDTHF,		sprite_resolution.x,
			DTA_VIRTUALHEIGHTF,		sprite_resolution.y);
		
		// amounts
		amount_text.Draw(
			text_resolution,
			button_to_text,
			ring_layout,
			1);
		
		// centre text
		let name_tick	= int(floor(name_counter.RealAmount(t)));
		let name_f		= name_t.RenderStandardDelta(name_fade_tics, t);
		let fade_f		= name_fade_t.RenderStandardDelta(name_fade_tics, t);
		
		Screen.DrawText(
			bigfont,
			Font.CR_SAPPHIRE,
			name_pos.x,
			name_pos.y,
			name_str,
			DTA_VIRTUALWIDTHF,		text_resolution.x,
			DTA_VIRTUALHEIGHTF,		text_resolution.y,
			DTA_ALPHA,				name_f,
			DTA_KEEPRATIO,			true,
			DTA_TEXTLEN,			name_tick);
		
		Screen.DrawText(
			bigfont,
			Font.CR_WHITE,
			name_pos.x,
			name_pos.y,
			name_str,
			DTA_VIRTUALWIDTHF,		text_resolution.x,
			DTA_VIRTUALHEIGHTF,		text_resolution.y,
			DTA_ALPHA,				fade_f * name_f,
			DTA_KEEPRATIO,			true,
			DTA_TEXTLEN,			name_tick);
	}

	// Reset helper vars.
	protected bool wants_quit;
	
	// Actually ticks the cursor.
	ui void TickCursor (int dx, int dy) {
		if(is_active) {
			ui_handling.mouse_input.NextState(dx, dy);
		}
	}
}

struct Gcq_AmountText play {
	private Array <String>	amount_str;
	private Array <double>	offset;
	private int				size;
	
	void MakeForInventory (Actor mo, Gcq_SelectableItems items) {
		if(items && mo) {
			ClearAllReserve(items.InventorySize());
			
			for(int i = 0; i < size; i++) {
				int f = items.Found(i);
				int amount;
				int max_amount;
				
				[amount, max_amount] = items.AmountAndMax(mo, f);
				
				if(max_amount <= 1) {
					amount_str [i] = "";
				}
				
				else {
					amount_str [i] = String.Format("%i \c[white]/ \c-%i",
						amount, max_amount);
				}
			}
		}
	}
	
	void SetOffsets (Font ffont, double f = 0.5) {
		for(int i = 0; i < size; i++) {
			offset [i] = f * ffont.StringWidth(amount_str [i]);
		}
	}
	
	private void ClearAllReserve (int amount) {
		amount_str.	clear();
		offset.		clear();
		amount_str.	reserve(amount);
		offset.		reserve(amount);
		size = amount;
	}
	
	ui void Draw (
	vector2				text_resolution,
	vector2				window_to_text,
	Gc_Shop_InvLayout	ring_layout,
	double				button_scale) const {
		let y_offset = button_scale * 20;
		
		for(let i = 0; i < size; i++) {
			let draw_pos	= ring_layout.DrawPos(i);
			draw_pos.x		*= window_to_text.x;
			draw_pos.y		*= window_to_text.y;
			
			Screen.DrawText(
				smallfont,
				Font.CR_FIRE,
				draw_pos.x - offset [i],
				draw_pos.y + y_offset,
				amount_str [i],
				DTA_VIRTUALWIDTHF,	text_resolution.x,
				DTA_VIRTUALHEIGHTF,	text_resolution.y,
				DTA_KEEPRATIO,		true);
		}
	}
}
